package com.java.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;
import com.java.business.dao.CategoryDao;
import com.java.business.entity.Category;
import com.java.business.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Category> page = this.page(
                new Query<Category>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<Category>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void deleteById(String id) {
        deleteChildren(id);
        this.baseMapper.deleteById(id);
    }

    private void deleteChildren(String id) {
        Category category = this.getById(id);
        if (category.getParent().equals("00000000")) {
            LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Category::getParent,category.getId());
            this.baseMapper.delete(queryWrapper);
        }
    }

}